package com.crr.controller;

import com.crr.entity.Emp;
import com.crr.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmpController {
    @Autowired
    private EmpMapper empMapper;

    @RequestMapping("/showInfo")
    public String show(Model model) {
        List<Emp> emps = empMapper.getAllEmp();
        model.addAttribute("emps", emps);
        return "show";
    }

    /* @RequestMapping("addEmp")
     public String addUser(Model model){
         Emp emp=new Emp(1,"zhansgan","保安",1.777777777,new Date());
         model.addAttribute("emp",emp);
         return "add";
     }*/

    /**
     * 增加员工信息
     *
     * @param emp
     * @return
     */
//    @RequestMapping("addA")
//    public String addEmp(Emp emp, HttpServletRequest request, Model model) {
//        Map<String, Object> map = new HashMap<>();
//        System.out.println(emp);
//        int res = empMapper.addEmp(emp);
//        if (res == 1) {
//            request.getSession().setAttribute("message", "用户添加成功");
//            return "redirect:showInfo";
//        } else {
//            model.addAttribute("message", "用户添加失败");
//            return "add";
//        }
//    }


    @RequestMapping("addA")
    public String addEmp(Emp emp,  Model model) {
        int res = empMapper.addEmp(emp);
        if (res == 1) {
            model.addAttribute("message", "用户添加成功");
            return "redirect:showInfo";
        } else {
            model.addAttribute("message", "用户添加失败");
            return "add";
        }
    }


    /**
     * 登录
     *
     * @param ename
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("loginck")
    public String addEmp(String ename, HttpSession session, Model model) {
        System.out.println(ename);
        Emp e = empMapper.login(ename);
        if (e != null) {
            session.setAttribute("ename", ename);
            return "redirect:showInfo";
        } else {
            model.addAttribute("msg", "用户不存在");
            /*session.setAttribute("msg", "用户不存在");*/
            return "login";
        }
    }
    /**
     * 删除员工信息
     * @param ename
     * @param model
     * @return
     */
    @RequestMapping("/del")
    public String delete(String ename,Model model){
       int res= empMapper.delEmp(ename);
       if(res==1){
           model.addAttribute("delmsg", "删除用户成功");
           return "delemp";
       }
       else{
           model.addAttribute("delmsg", "删除用户失败");
           return "show";
       }

    }
    @RequestMapping("/udt")
    public String update(Emp emp,Model model){
       int res= empMapper.updateEmp(emp);
        if(res==1){
            model.addAttribute("udtmsg", "用户修改成功");
            return "udtemp";
        }
        else{
            model.addAttribute("udtmsg", "用户修改失败");
            return "show";
        }

    }

    /**
     * 进入登录页面
     * @return
     */
    @RequestMapping("/lin")
    public String login() {
        return "login";
    }

    /**
     * 进入到新增页面
     * @return
     */
    @RequestMapping("addck")
    public String addck(){
        return "add";
    }

    /**
     * 进入修改页面
     * @return
     */
    @RequestMapping("")
    public String updateck(){
        return "udtemp";
    }

}
